package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.ActivityCouponsDTO;
import com.sc.nft.entity.dto.ActivityCouponsDetailDTO;
import com.sc.nft.entity.dto.ActivityCouponsProductDTO;
import com.sc.nft.entity.vo.ActivityCouponsProductVO;
import com.sc.nft.entity.vo.MallActivityCouponsListVO;
import com.sc.nft.entity.vo.UserReceiveVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.ActivityCouponsService;
import com.sc.nft.service.ActivityCouponsUserService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author liGen
 * @version 1.0
 * @date 2023/12/6 3:50 下午
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/activity/coupons")
@Api(tags = "优惠券模块")
public class ActivityCouponsController {

    private final ActivityCouponsService activityCouponsService;
    private final ActivityCouponsUserService activityCouponsUserService;
    private final Redisson redisson;


    @ApiOperation(value = "优惠券活动列表", notes = "优惠券活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<ActivityCouponsDTO> activityCouponsPage(@RequestBody MallActivityCouponsListVO mallActivityCouponsListVO) {
        return PageResult.success(activityCouponsService.getMallActivityCouponsDTOPage(mallActivityCouponsListVO.getPageNo(), mallActivityCouponsListVO.getPageSize(), UserPool.getUser(),mallActivityCouponsListVO.getActivityProductId()));
    }

    @ApiOperation(value = "优惠券活动详情", notes = "优惠券活动详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/details", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<ActivityCouponsDetailDTO> details(@Valid @RequestBody IdRequest idRequest) {
        ActivityCouponsDetailDTO activityCouponsDetailDTO = activityCouponsService.getActivityCouponsDetailDTOById(idRequest.getId());
        return Result.success(activityCouponsDetailDTO);
    }

    @ApiOperation(value = "优惠券下可用的商品列表", notes = "优惠券下可用的商品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/productList", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<ActivityCouponsProductDTO> getActivityCouponsProductDTOPage(@Valid @RequestBody ActivityCouponsProductVO activityCouponsProductVO) {
        return PageResult.success(activityCouponsService.getActivityCouponsProductDTOPage(activityCouponsProductVO));
    }

    @ApiOperation(value = "用户领取优惠券活动", notes = "用户领取优惠券活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userReceive", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userReceive(@Valid @RequestBody UserReceiveVO userReceiveVO) {
        UserInfo userInfo = UserPool.getUser();
        // 用户领取优惠券
        RLock lock = redisson.getLock(RedisKeyEnum.USER_RECEIVES_COUPON_LOCK.getKey() + userInfo.getId());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                activityCouponsUserService.userReceive(userReceiveVO.getId(),userInfo,userReceiveVO.getMemberId(),userReceiveVO.getMemberEquityTemplateId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("领取中，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }
}
